<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\Response;
use app\models\LoginForm;
use app\models\ContactForm;
use app\services\SolutionService;

class CreativeController extends AuthController
{
    public $currentHeader = 'header-campaign';
    public $currentNav = 'creative-nav';
    public $pageTitle = '';

    public function init() {
        parent::init();
        //替换为正确的网络
        $adnetworkId = $this->S('ad_network_id');
        $this->categories = $this->service->getAllCategories($adnetworkId); 
        $this->templates = $this->getAll('CreativeTemplate', ['is_sl' => 1]);
        $this->tanxAds = $this->service->getTanxAds();
    }

    public function actionCreativeList() {
        $this->pageTitle = $this->t('creative', 'Creative List');
        $this->solution_id = $this->I('solution_id');
        $dataFilter = 'true';
        if (in_array($this->S('user_rank'), ['agent', 'advertiser'])) $dataFilter = '';
        $this->dataFilter = $dataFilter;
        $this->filter = $this->isAllowedSeeShows();
        $this->brandShow = true;
        return $this->show();
    }

    public function actionNewOverlayCreative() {
        $this->pageTitle = $this->t('creative', 'New Overlay Creative');
        $adnetworkId = $this->S('ad_network_id');
        $solutionId = $this->I('solution_id');
        $this->solution_id = $solutionId;
        $this->S('solution_id', null);
        $this->vsizes = $this->service->getAvailableCreativeSize('VideoAdspace', $adnetworkId, $this->I('solution_id'), 4);
        return $this->show();
    }

    public function actionUploadOverlay() {
        $adnetworkId = $this->S('ad_network_id');
        $vsizes = $this->service->getAvailableCreativeSize('VideoAdspace', $adnetworkId, $this->I('solution_id'), 4);

        $fileInfo = $this->service->checkOverlayMaterail();
        if (!$fileInfo) {
            $this->NG($this->t('creative', 'Material not qualified!'));
        }
        $solutionId = $this->I('solution_id');
        $advertiserId = $this->S('advertiser_id');
        //set duration
        $fileInfo[0]['duration'] = 8; 
        $result = $this->service->addMaterial($fileInfo, $solutionId, $advertiserId);
        if (is_array($result)) {
            $result = $this->service->getRef($result, 'VideoAdspace');
            if ($result['table']['judgeInfo']  == $this->t('creative', 'not qualified')) {
                $this->NG($this->t('creative', 'Material not qualified!'));
            }
            $this->OK($this->t('creative', 'Update material successfully!'), 'ok', $result);
        }
        $this->NG($result);
    }

    public function actionNewOverlayCreativeHandle() {
        $creative = $this->I();
        if ($this->service->saveNewCreative($creative, 'video', 'overlay')) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionUpload() {
        $id = $this->I('creative_id');
        $result = $this->service->uploadCreative($id);
        $this->jsonReturn(['data' => $result]);
    }

    public function actionUpdate() {
        $id = $this->I('creative_id');
        $result = $this->service->updateCreative($id);
        $this->jsonReturn(['data' => $result]);
    }

    public function actionGetStatus() {
        $id = $this->I('creative_id');
        $result = $this->service->updateCreativeAuditStatus($id);
        $this->jsonReturn(['data' => $result]);
    }

    public function actionNewCreativeHandle() {
        $creative = $this->I();
        if ($this->service->saveNewCreative($creative)) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionNewVideoCreativeHandle() {
        $creative = $this->I();
        if ($this->service->saveNewCreative($creative, 'video')) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionNewCpeBannerCreativeHandle() {
        $creative = $this->I();
        if ($result = $this->service->saveNewCreative($creative, 'cpe_banner')) {
            $this->OK($this->t('creative', 'Save creative successfully!'),'',$result);
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionNewCpeVideoCreativeHandle() {
        $creative = $this->I();
        if ($this->service->saveNewCreative($creative, 'cpe_video')) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionCreativeEdit() {
        $this->pageTitle = $this->t('creative', 'Edit Display Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->pic = $creative['file_url'];
        $this->sizes = $this->service->getAvailableCreativeSize('Adspace', '', $this->I('solution_id'));
        return $this->show();
    }

    public function actionVideoCreativeEdit() {
        $this->pageTitle = $this->t('creative', 'Edit Video Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->videoUrl = $creative['file_url'];
        $this->sizes = $this->service->getAvailableCreativeSize('VideoAdspace', '', $this->I('solution_id'));
        if ($creative['type'] == 2) {
            $adnetworkId = $this->S('ad_network_id');
            $this->sizes = $this->service->getAvailableCreativeSize('VideoAdspace', '', $this->I('solution_id'), 'overlay');
            return $this->show('overlayCreativeEdit');
        }
        return $this->show();
    }

    public function actionCpeBannerCreativeEdit() {
        $this->pageTitle = $this->t('creative', 'Edit CPE Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->pic = $creative['file_url'];
        $this->sizes = $this->service->getAvailableCreativeSize('Adspace', '', $this->I('solution_id'),'cpe-banner');
        return $this->show();
    }

    public function actionCpeVideoCreativeEdit() {
        $this->pageTitle = $this->t('creative', 'Edit CPE Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $this->sizes = $this->service->getAvailableCreativeSize('VideoAdspace', '', $this->I('solution_id'));
        return $this->show();
    }

    public function actionUploadPic() {
        $this->service->uploadBannerPic();
    }

    public function actionUploadVideo() {
        $this->service->uploadVideoPic('VideoAdspace');
    }

    public function actionGetMaterialPicture() {
        $name = $this->I('name');
        $this->service->getPicture($name);
    }

    public function actionGetCreativesList() {
        $crts = $this->service->getCreativesList();
        $this->jsonReturn(['data' => $crts]);
    }

    public function actionGetSolutionUnbindCreative() {
        $solutionId = $this->I('solution_id');
        $crts = $this->service->getSolutionUnbindCreative($solutionId);
        $this->jsonReturn(['data' => $crts]);
    }

    public function actionListCreativePreview () {
        $this->creativePreview();
    }

    public function actionListVideoCreativePreview () {
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->videoUrl = $creative['file_url'];
        $this->videoData = $this->service->getVideoData($material, $creative);
        $this->layout = 'blank';
        return $this->show();
    }

    public function actionListCpeCreativePreview () {
        $creativeId = $this->I('id');
        $this->creative_id = $creativeId;
        $this->preview = $this->service->getCpeCreativePreview($creativeId);
        $this->layout = 'blank';
        return $this->show();
    }

    public function actionCpePreview(){
       $creativeId = $this->I('guid');
       $preview = $this->service->getCpeCreativePreview($creativeId);
       return json_encode($preview);
    }

    public function actionListOperateHandle() {
        $creativeId = $this->I('creative_id');
        $type = $this->I('type');
        if ($this->service->updateCreativeStatus($creativeId, $type)) {
            $this->OK($this->t('creative', 'Action done!'));
        }
        $this->NG($this->t('creative', 'Action failed!'));
    }

    public function actionGetCreativeBindSolutions() {
        $creativeId = $this->I('id');
        $solutions = $this->service->getCreativeBindSolutions($creativeId);
        $this->jsonReturn($solutions);
    }

    public function actionNewWebCreative() {
        $this->pageTitle = $this->t('creative', 'New Web Creative');
        $this->solution_id = $this->I('solution_id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        $this->sizes = $this->service->getAvailableCreativeSize('MobileAdspace', '', $this->I('solution_id'), 'web');
        return $this->show();
    } 
    public function actionNewAppCreative() {
        $this->pageTitle = $this->t('creative', 'New App Creative');
        $this->solution_id = $this->I('solution_id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        $service = new SolutionService();
        $solutionId = $this->I('solution_id');
        $bwList = $service->getConditionBWList($solutionId, 'is_for_mobile');
        $conditions = $service->getConditionsKey($solutionId, 'is_for_mobile');
        $type = 'no_limit';
        if (isset($bwList['ConditionAdspace'])) {
            $type = $bwList['ConditionAdspace'];
        }
        $ads = [];
        if (isset($conditions['ConditionAdspace'])) {
            $ads = $conditions['ConditionAdspace'];
        }
        $sizes = $this->service->getTargetedAppAllowedSizes($ads, $type);
        $this->sizes = $sizes;
        $this->formats = $this->service->getTargetedAdspaceFormats($ads, $type);
        return $this->show();
    }

    public function actionUploadMobileWebPic() {
        $this->service->uploadMobileWebPic();   
    }

    public function actionUploadMobileAppPic() {
        $this->service->uploadMobileAppPic();
    }

    public function actionNewMobileWebCreativeHandle() {
        $creative = $this->I();
        if (!$creative['upload_material'] && !$creative['custom_url']) {
            $this->NG($this->t('creative', 'Creative has no material'));
        }

        if ($this->service->saveNewCreative($creative, 'mobile', 'web')) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionNewMobileAppCreativeHandle() {
        $creative = $this->I(); 
        if ($creative['ad_type'] != 3 && ((isset($creative['upload_material']) && !$creative['upload_material']) || !$creative['custom_url'])) {
            $this->NG($this->t('creative', 'Creative has no material!'));
        } else if ($creative['ad_type'] == 3) {
            if(!$creative['title'] && !$creative['txt'] && (!isset($creative['upload_material']) && !$creative['custom_url'])) {
                $this->NG($this->t('creative', 'Creative title or description or material cannot be empty!'));
            }
        }

        if ($this->service->saveNewCreative($creative, 'mobile', 'app')) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionWebCreativeEdit() {
        $this->pageTitle = $this->t('creative', 'Edit Web Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->pic = $creative['file_url'];
        $this->sizes = $this->service->getAvailableCreativeSize('MobileAdspace', '', $this->I('solution_id'), 'web');
        return $this->show();
    }

    public function actionAppCreativeEdit() {
        $this->pageTitle = $this->t('creative', 'Edit App Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $this->materials = $this->service->getCreativeMaterials($creative);
        $this->sizes = $this->service->getAvailableCreativeSize('MobileAdspace', '', $this->I('solution_id'), 'app');
        return $this->show('appCreativeEdit');
    }

    public function actionListAppCreativePreview () {
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->videoUrl = $creative['file_url'];
        $this->layout = 'blank';
        return $this->show();
    }

    public function actionNewBannerCreative() {
        $this->pageTitle = $this->t('creative', 'New Display Creative');
        $this->solution_id = $this->I('solution_id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        $this->sizes = $this->service->getAvailableCreativeSize('Adspace', '', $this->I('solution_id'));
        return $this->show();
    } 

    public function actionNewVideoCreative() {
        $this->pageTitle = $this->t('creative', 'New Video Creative');
        $this->solution_id = $this->I('solution_id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        $this->sizes = $this->service->getAvailableCreativeSize('VideoAdspace', '', $this->I('solution_id'));
        return $this->show();
    }

    public function actionNewMobileVideoCreative() {
        $this->pageTitle = $this->t('creative', 'New Mobile Video Creative');
        $this->solution_id = $this->I('solution_id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        $this->sizes = $this->service->getAvailableCreativeSize('MobileVideoAdspace', '', $this->I('solution_id'));
        return $this->show();
    }

    public function actionUploadMobileVideo() {
        $this->service->uploadVideoPic('MobileVideoAdspace');
    }

    public function actionNewMobileVideoCreativeHandle() {
        $creative = $this->I();
        if ($this->service->saveNewCreative($creative, 'mobileVideo')) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionMobileVideoCreativeEdit() {
        $this->pageTitle = $this->t('creative', 'Edit Mobile Video Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->videoUrl = $creative['file_url'];
        $this->sizes = $this->service->getAvailableCreativeSize('MobileVideoAdspace', '', $this->I('solution_id'));
        return $this->show();
    }

    public function actionListMobileVideoCreativePreview()
    {
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $material = $this->M('Material', $creative['material_id']);
        $this->material = $material;
        $this->videoUrl = $creative['file_url'];
        $this->videoData = $this->service->getVideoData($material, $creative);
        $this->layout = 'blank';
        return $this->show();
    }

    // New CPE Creative
    public function actionNewCpeBannerCreative() {
        $this->pageTitle = $this->t('creative', 'New CPE Creative');
        $this->solution_id = $this->I('solution_id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        $this->sizes = $this->service->getAvailableCreativeSize('Adspace', '', $this->I('solution_id'),'cpe-banner');
        return $this->show();
    }

    public function actionNewCpeVideoCreative() {
        $this->pageTitle = $this->t('creative', 'New CPE Creative');
        $this->solution_id = $this->I('solution_id');
        $this->creative_id = $this->I('id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        // $this->sizes = $this->service->getAvailableCreativeSize('Adspace', '', $this->I('solution_id'));
        return $this->show();
    }
    public function actionNewCustomCreative()
    {
        $this->pageTitle = $this->t('creative', 'New Custom Creative');
        $this->solution_id = $this->I('solution_id');
        $this->S('solution_id', null);
        $this->S('ad_network_id', null);
        $this->sizes = $this->service->getAvailableCreativeSize('Adspace', '', $this->I('solution_id'));
        return $this->show();
    } 

    public function actionCustomCreativeEdit()
    {
        $this->pageTitle = $this->t('creative', 'Edit Custom Creative');
        $crid = $this->I('id');
        $creative = $this->M('Creative', $crid);
        $this->creative = $creative;
        $category = $this->service->getCategoryNameById($creative['category_id']);
        $this->category = $category;
        $this->sizes = $this->service->getAvailableCreativeSize('Adspace', '', $this->I('solution_id'));
        return $this->show();
    }

    public function actionNewCustomCreativeHandle()
    {
        $creative = $this->I();
        if ($this->service->saveCustomCreative($creative)) {
            $this->OK($this->t('creative', 'Save creative successfully!'));
        }
        $this->NG($this->t('creative', 'Save creative failed!'));
    }

    public function actionEditCustomCreativeHandle()
    {
        $this->actionNewCustomCreativeHandle();
    }

}// end of creative controller
